import { Input, Button, Form,message} from 'antd';
import { LockOutlined } from '@ant-design/icons';
import {changeAPI,} from "@/services/ant-design-pro/changePassword";

import { history } from 'umi';
import styles from "@/pages/colony/changePassword/index.less";

const Formt = () => {
    const [form] = Form.useForm();
    const handleSubmit = async (values: any) => {

        const params = {
            oldPwd: values.old_password,
            newPwd: values.new_password

        }
        const result = await changeAPI(params);
        if(result.success){
            message.success("修改密码成功");

            history.push("/user/login");
        }
        else{message.error("原密码错误")};


    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);

    };

    return (
        <div className="changeForm">
            <Form

                form={form}
                name="basic"
                onFinish={handleSubmit}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >


                <Form.Item

                    label="原密码"
                    name="old_password"
                    rules={[
                        { required: true, message: '请输入原密码！' },

                    ]}
                >
                    <Input.Password prefix={<LockOutlined />} />
                </Form.Item>

                <Form.Item

                    label="新密码"
                    name="new_password"
                    rules={[
                        { required: true, message: '请输入新密码！' },
                        { min: 5, message: '密码长度不能少于5个字符！' },

                    ]}
                >

                    <Input.Password prefix={<LockOutlined />} />
                </Form.Item>

                <Form.Item

                    label="确认密码"
                    name="confirm_password"
                    dependencies={['new_password']} // 设置依赖项为"new_password"
                    rules={[
                        { required: true, message: '请确认密码！' },
                        ({ getFieldValue }) => ({
                            validator(_, value) {
                                if (!value || getFieldValue('new_password') === value) {
                                    return Promise.resolve();
                                }
                                return Promise.reject('两次输入的密码不一致！');
                            },
                        }),
                    ]}
                >
                    <Input.Password prefix={<LockOutlined />} />
                </Form.Item>


                <Form.Item>
                    <Button type="primary" htmlType="submit" className={styles.btnItem}>
                        修改密码
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
};

export default Formt;